package com.zyl.king;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author zhouleibin
 * @Type
 * @Desc
 * @date 2020-08-27 20:03
 */
public class CodeGenerator {

    private static final String author = "zyl";//作者


    private static final String url = "jdbc:mysql://127.0.0.1:3306/king?useUnicode=true&characterEncoding=UTF-8";//数据库
    private static final String driverName = "com.mysql.cj.jdbc.Driver";//驱动
    private static final DbType dbType = DbType.MYSQL;//驱动
    //private static final String url = "jdbc:oracle:thin:@172.1.1.44:1521:ecm";//数据库
    //private static final String driverName = "oracle.jdbc.driver.OracleDriver";//驱动
    private static final String userName = "root";
    private static final String password = "123456";
    private static final String projectName = "king";//项目名称
    private static final String packageName = "com.zyl";//包名
    private static final String[] tableName = new String[]{
    };

    public static void main(String[] args) {
        CodeGenerator g = new CodeGenerator();
        g.generateByTables(projectName, packageName, tableName);
    }

    /**
     * 根据表自动生成
     *
     * @param packageName 包名
     * @param tableNames  表名
     */
    private void generateByTables(String projectName, String packageName, String... tableNames) {

        String projectPath = System.getProperty("user.dir");
        FastAutoGenerator.create(url, userName, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .outputDir(projectPath + "/" + projectName + "/src/main/java/"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent(packageName) // 设置父包名
                                .entity("po")
                                .moduleName(projectName) // 设置父包模块名
                                .mapper("mapper")
                                .service("service")
                                .serviceImpl("service.impl")
                                .xml("mapper.xml")
                                .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/" + projectName + "/src/main/resources/mapper")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude(tableNames)// 设置需要生成的表名
                                .entityBuilder()
                                .enableLombok()
                                .addTableFills(
                                        new Column("create_time", FieldFill.INSERT)
                                )
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
